package wec_wallet.ebo.com.activity.main;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.support.annotation.Nullable;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import bassproject.ebo.com.ebobass.activity.TakePhotoFragment;
import bassproject.ebo.com.ebobass.utils.IToast;
import bassproject.ebo.com.ebobass.utils.I_Share;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;
import wec_wallet.ebo.com.common.Constant;
import wec_wallet.ebo.com.common.HttpUtils;
import wec_wallet.ebo.com.utils.Functions;

/**
 * Created by admin on 2018/4/13.
 */

public class AbsImageFragment extends TakePhotoFragment {

    protected Handler mHandler;



    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mHandler = new Handler(Looper.getMainLooper()) {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                if (msg.obj == null) return;
                handleMsg(msg);
            }
        };
    }

    protected void handleMsg(Message msg) {

    }


    protected String[] parseThumb(String result) {

        //文件上传
        JSONObject resultObject;
        try {
            resultObject = new JSONObject(result);
            String status = resultObject.getString("error");
            if (resultObject.has("is_auth") && resultObject.getString("is_auth").equals
                    ("0")) {
                IToast.show(resultObject.getString("error_msg"));
                Functions.relogin(getActivity());//登录过期

                return null;
            }
            if (resultObject.has("error_msg")) {
                IToast.show(resultObject.getString("error_msg"));
                return null;
            }
            if (status.equals("0")) {
                JSONArray dataObject = new JSONArray(resultObject.getString("data"));
                JSONObject string = (JSONObject) dataObject.get(0);
                JSONObject extra = string.getJSONObject("extra");
                String path = extra.getString("path");
                String name = extra.getString("name");

                return new String[]{path, name};

            } else {

            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return null;
    }

    protected void uploadAvatar(String filePath) {
        Map<String, String> maps = new HashMap<>();
        maps.put("uid", I_Share.readAccountUserid());
        maps.put("token", I_Share.readAccountToken());

        try {
            HttpUtils.uploadFile(Constant.URL_UPLOAD, "thumb", filePath, maps, new Callback() {
                @Override
                public void onFailure(Call call, IOException e) {

                }

                @Override
                public void onResponse(Call call, Response response) throws IOException {
                    if (response.isSuccessful()) {
                        Message msg = Message.obtain(mHandler);
                        msg.what = 0;
                        msg.obj = response.body().string();
                        mHandler.sendMessage(msg);
                    }


                }
            });
        } catch (IOException e) {
            e.printStackTrace();
        }

    }


    @Override
    public void onFetchPhotoPostExcute(Bitmap bitmap, String orignalPath, String cropPath) {
        super.onFetchPhotoPostExcute(bitmap, orignalPath, cropPath);
    }
}
